import java.text.SimpleDateFormatclass RegisterController {

    def index = { }
    
    def nAirport = {		flash.message = ""				if (params.actionType == "submit") {
    		def person = Sportsman.get(params.personId)
        	def calendar = Calendar.getInstance()    		person.nAirportComeAt = calendar.getTime()    		calendar.add(Calendar.MINUTE, 30)    		person.predictNTrainComeAt = calendar.getTime()
        	if (person.save()) {
        		def formatedDate = formatDate(person.nAirportComeAt)
        		flash.message = "運動員 ${person.name} 在 ${formatedDate} 抵達桃園機場"        	}
		}		def allCount = Sportsman.count() - Sportsman.findAll("from Sportsman as s where s.nAirportComeAt > ?",[new Date(0)]).size()    	[people:searchPeople("nAirport"), allCount:allCount, searchType:params.searchType, searchKey:params.searchKey]
    }        def nTrain = {		flash.message = ""				if (params.actionType == "submit") {			def person = Sportsman.get(params.personId)        	def calendar = Calendar.getInstance()    		person.nTrainComeAt = calendar.getTime()    		calendar.add(Calendar.MINUTE, 100)    		person.predictSTrainComeAt = calendar.getTime()        	if (person.save()) {        		def formatedDate = formatDate(person.nTrainComeAt)        		flash.message = "運動員 ${person.name} 在 ${formatedDate} 抵達桃園高鐵站 "        	}		} else if (params.actionType == "delete") {			def person = Sportsman.get(params.personId)        	person.nAirportComeAt = new Date(0)        	person.save()        	flash.message = "已移除運動員 ${person.name} 紀錄"		}		def allCount = Sportsman.findAll("from Sportsman as s where s.nAirportComeAt > ? and s.nTrainComeAt = ?",[new Date(0), new Date(0)]).size()    	[people:searchPeople("nTrain"), allCount:allCount, searchType:params.searchType, searchKey:params.searchKey]    }        def sTrain = {		flash.message = ""				if (params.actionType == "submit") {			def person = Sportsman.get(params.personId)        	def calendar = Calendar.getInstance()    		person.sTrainComeAt = calendar.getTime()    		calendar.add(Calendar.MINUTE, 60)    		person.predictCenterComeAt = calendar.getTime()        	if (person.save()) {        		def formatedDate = formatDate(person.sTrainComeAt)        		flash.message = "運動員 ${person.name} 在 ${formatedDate} 抵達左營高鐵站 "        	}		} else if (params.actionType == "delete") {			def person = Sportsman.get(params.personId)        	person.nTrainComeAt = new Date(0)        	person.save()        	flash.message = "已移除運動員 ${person.name} 紀錄"		}		def allCount = Sportsman.findAll("from Sportsman as s where s.nTrainComeAt > ? and s.sTrainComeAt = ?",[new Date(0), new Date(0)]).size()    	[people:searchPeople("sTrain"), allCount:allCount, searchType:params.searchType, searchKey:params.searchKey]    }        def center = {		flash.message = ""				if (params.actionType == "submit") {			def person = Sportsman.get(params.personId)        	def calendar = Calendar.getInstance()    		person.centerComeAt = calendar.getTime()    		calendar.add(Calendar.MINUTE, 30)    		person.predictHotelComeAt = calendar.getTime()        	if (person.save()) {        		def formatedDate = formatDate(person.centerComeAt)        		flash.message = "運動員 ${person.name} 在 ${formatedDate} 抵達認證中心 "        	}		} else if (params.actionType == "delete") {			def person = Sportsman.get(params.personId)        	person.sTrainComeAt = new Date(0)        	person.save()        	flash.message = "已移除運動員 ${person.name} 紀錄"		}		def allCount = Sportsman.findAll("from Sportsman as s where s.sTrainComeAt > ? and s.centerComeAt = ?",[new Date(0), new Date(0)]).size()    	[people:searchPeople("center"), allCount:allCount, searchType:params.searchType, searchKey:params.searchKey]    }        def hotel = {		flash.message = ""				if (params.actionType == "submit") {			def person = Sportsman.get(params.personId)        	def calendar = Calendar.getInstance()    		person.hotelComeAt = calendar.getTime()        	if (person.save()) {        		def formatedDate = formatDate(person.hotelComeAt)        		flash.message = "運動員 ${person.name} 在 ${formatedDate} 抵達旅館 "        	}		} else if (params.actionType == "delete") {			def person = Sportsman.get(params.personId)        	person.centerComeAt = new Date(0)        	person.save()        	flash.message = "已移除運動員 ${person.name} 紀錄"		}		def allCount = Sportsman.findAll("from Sportsman as s where s.centerComeAt > ? and s.hotelComeAt = ?",[new Date(0), new Date(0)]).size()    	[people:searchPeople("hotel"), allCount:allCount, searchType:params.searchType, searchKey:params.searchKey]    }        def hotelLeft = {    		flash.message = ""    		    		if (params.actionType == "submit") {    			def person = Sportsman.get(params.personId)            	def calendar = Calendar.getInstance()        		person.hotelGoAt = calendar.getTime()        		calendar.add(Calendar.MINUTE, 60)        		person.predictSTrainGoAt = calendar.getTime()            	if (person.save()) {            		def formatedDate = formatDate(person.hotelGoAt)            		flash.message = "運動員 ${person.name} 在 ${formatedDate} 離開旅館 "            	}    		}			def allCount = Sportsman.count() - Sportsman.findAll("from Sportsman as s where s.hotelGoAt > ?",[new Date(0)]).size()        	[people:searchPeople("hotelLeft"), allCount:allCount, searchType:params.searchType, searchKey:params.searchKey]        }    
    // methods ------------------    def formatDate(date) {    	def dateFormat = new SimpleDateFormat("MM-dd HH:mm")    	return dateFormat.format(date)    }    
    def searchPeople(type) {
    	def people = []	
    	if (params.searchType == "name") {
    		people = Sportsman.findAllByNameLike('%'+ params.searchKey +'%',[sort:"comeDateAt"])
    	} else if (params.searchType == "uid") {
    		people = Sportsman.findAllByUidLike('%'+ params.searchKey +'%',[sort:"comeDateAt"])
    	} else if (params.searchType == "nation") {
    		def nation = Nation.get(params.searchKey)
    		people = Sportsman.findAllByNation(nation,[sort:"comeDateAt"])
    	} else if (params.searchType == "sport") {
    		def sport = Sport.get(params.searchKey)
    		people = Sportsman.findAllBySport(sport,[sort:"comeDateAt"])
    	}    	
    	if (type == "nAirport") {
    		return people.findAll{it.nAirportComeAt.getYear() == 70}
    	} else if (type == "nTrain") {    		return people.findAll{it.nAirportComeAt.getYear() > 70}    					.findAll{it.nTrainComeAt.getYear() == 70}    	} else if (type == "sTrain") {    		return people.findAll{it.nTrainComeAt.getYear() > 70}						.findAll{it.sTrainComeAt.getYear() == 70}    	} else if (type == "center") {    		return people.findAll{it.sTrainComeAt.getYear() > 70}						.findAll{it.centerComeAt.getYear() == 70}    	} else if (type == "hotel") {    		return people.findAll{it.centerComeAt.getYear() > 70}						.findAll{it.hotelComeAt.getYear() == 70}    	} else if (type == "hotelLeft") {    		return people.findAll{it.hotelGoAt.getYear() == 70}    	}
    }
}
